//package com.cn.lxz.android.aws;
//
//import android.os.Bundle;
//import android.util.Log;
//
//import androidx.appcompat.app.AppCompatActivity;
//
//import com.amazonaws.AmazonClientException;
//import com.amazonaws.auth.AWSCredentials;
//import com.amazonaws.auth.BasicAWSCredentials;
//import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
//import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
//import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
//import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
//import com.amazonaws.regions.Region;
//import com.amazonaws.regions.Regions;
//import com.amazonaws.services.s3.AmazonS3;
//import com.amazonaws.services.s3.AmazonS3Client;
//
//import java.io.ByteArrayInputStream;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.ArrayList;
//import java.util.List;
//
//public class S3UploadActivity extends AppCompatActivity {
//
//    private static final String TAG = "S3UploadActivity";
//    private static final String ACCESS_KEY = "your_access_key";
//    private static final String SECRET_KEY = "your_secret_key";
//    private static final String BUCKET_NAME = "your_bucket_name";
//    private static final String OBJECT_KEY = "your_object_key";
//    private static final long FILE_SIZE = 2 * 1024 * 1024 * 1024; // 2GB
//    private static final long CHUNK_SIZE = 5 * 1024 * 1024; // 5MB per chunk
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_s3_upload);
//
//        AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
//        AmazonS3 s3Client = new AmazonS3Client(credentials);
//        s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
//        TransferUtility transferUtility = TransferUtility.builder()
//                .s3Client(s3Client)
//                .context(getApplicationContext())
//                .build();
//
//        try {
//            File file = new File("path_to_your_2GB_file");
//            long totalChunks = FILE_SIZE / CHUNK_SIZE;
//            if (FILE_SIZE % CHUNK_SIZE!= 0) {
//                totalChunks++;
//            }
//
//            List<TransferObserver> observers = new ArrayList<>();
//            for (int i = 0; i < totalChunks; i++) {
//                long start = i * CHUNK_SIZE;
//                long end = Math.min(start + CHUNK_SIZE - 1, FILE_SIZE - 1);
//                try (InputStream inputStream = new FileInputStream(file)) {
//                    inputStream.skip(start);
//                    byte[] buffer = new byte[(int) (end - start + 1)];
//                    int bytesRead = inputStream.read(buffer);
//                    if (bytesRead > 0) {
//                        ByteArrayInputStream bufferInputStream = new java.io.ByteArrayInputStream(buffer);
//                        TransferObserver observer = transferUtility.upload(BUCKET_NAME,
//                                OBJECT_KEY + "_part" + i,
//                                bufferInputStream);
//                        observers.add(observer);
//                        int finalI = i;
//                        observer.setTransferListener(new TransferListener() {
//                            @Override
//                            public void onStateChanged(int id, TransferState state) {
//                                Log.d(TAG, "Transfer state changed for part " + finalI + ": " + state);
//                            }
//
//                            @Override
//                            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
//                                Log.d(TAG, "Progress for part " + finalI + ": " + bytesCurrent + "/" + bytesTotal);
//                            }
//
//                            @Override
//                            public void onError(int id, Exception ex) {
//                                Log.e(TAG, "Error for part " + finalI + ": ", ex);
//                            }
//                        });
//                    }
//                } catch (FileNotFoundException e) {
//                    Log.e(TAG, "File not found for part " + i, e);
//                } catch (IOException e) {
//                    Log.e(TAG, "IO error for part " + i, e);
//                }
//            }
//
//            // Wait for all parts to complete
//            boolean allCompleted = false;
//            while (!allCompleted) {
//                allCompleted = true;
//                for (TransferObserver observer : observers) {
//                    if (observer.getState()!= TransferState.COMPLETED) {
//                        allCompleted = false;
//                        break;
//                    }
//                }
//            }
//
//            Log.d(TAG, "All parts uploaded successfully.");
//
//        } catch (AmazonClientException e) {
//            Log.e(TAG, "AWS client exception", e);
//        } catch (IOException e) {
//            Log.e(TAG, "IO exception", e);
//        }
//    }
//}